import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/home',
    // meta:{ title: '首页'},
    component: () => import('@/layout/Layout.vue'),
    children: [
      {
        path: '',
        // meta:{ title: '首页'},
        component: () => import('@/views/home/Home.vue')
      }
    ]
  },
  {
    path: '/goods',
    redirect: '/goods/list'
  },
  {
    path: '/goods',
    meta: { title: '商品管理' },
    component: () => import('@/layout/Layout.vue'),
    children: [
      {
        path: '/goods/add',
        meta: { title: '商品添加' },
        component: () => import('../views/goods/GoodsAdd.vue')
      },
      {
        path: '/goods/list',
        meta: { title: '商品列表' },
        component: () => import('../views/goods/GoodsList.vue')
      },
      {
        path: '/goods/classify',
        meta: { title: '商品分类' },
        component: () => import('../views/goods/GoodsClassify.vue')
      }
    ]
  },
  {
    path: '/accout',
    redirect: '/accout/list'
  },
  {
    path: '/accout',
    meta: { title: '账号管理' },
    component: () => import('@/layout/Layout.vue'),
    children: [
      {
        path: '/account/add',
        meta: { title: '账号添加' },
        component: () => import('../views/account/AccountAdd.vue')
      },
      {
        path: '/account/list',
        meta: { title: '账号列表' },
        component: () => import('../views/account/AccountList.vue')
      },
      {
        path: '/account/change',
        meta: { title: '修改密码' },
        component: () => import('../views/account/ChangePwd.vue')
      },
      {
        path: '/account/mine',
        meta: { title: '个人中心' },
        component: () => import('../views/account/Mine.vue')
      }
    ]
  },
  {
    path: '/order',
    meta: { title: '订单' },
    component: () => import('@/layout/Layout.vue'),
    children: [
      {
        path: '',
        // meta:{ title: '订单'},
        component: () => import('../views/order/order.vue')
      }
    ]
  },
  {
    path: '/sale',
    meta: { title: '销售统计' },
    component: () => import('@/layout/Layout.vue'),
    children: [
      {
        path: '/sale/goods',
        meta: { title: '商品统计' },
        component: () => import('../views/sale/GoodsStatistics.vue')
      },
      {
        path: '/sale/order',
        meta: { title: '订单统计' },
        component: () => import('../views/sale/OrderStatistics.vue')
      },
    ]
  },
  {
    path: '/shop',
    component: () => import('@/layout/Layout.vue'),
    children: [
      {
        path: '',
        meta: { title: '店铺管理' },
        component: () => import('../views/shop/shop.vue')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('../views/login/login.vue')
  },
  {
    path: '/*',
    component: () => import('../views/error/error.vue')
  }
]

//静态路由
const staticRoutes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/home',
    meta: { title: '首页', menu: true },
    component: () => import('@/layout/Layout.vue'),
    children: [
      {
        path: '',
        // meta:{ title: '首页'},
        component: () => import('@/views/home/Home.vue')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('../views/login/login.vue')
  },
  // {
  //   path: '/*',
  //   component: () => import('../views/error/error.vue')
  // }
]

//普通管理员路由
// const normalRoutes = [
//   {
//     path: '/home',
//     // meta:{ title: '首页'},
//     component: () => import('@/layout/Layout.vue'),
//     children: [
//       {
//         path: '',
//         // meta:{ title: '首页'},
//         component: () => import('@/views/home/Home.vue')
//       }
//     ]
//   },
//   {
//     path: '/goods',
//     redirect: '/goods/list'
//   },
//   {
//     path: '/accout',
//     redirect: '/accout/list'
//   },
//   {
//     path: '/accout',
//     meta: { title: '账号管理' },
//     component: () => import('@/layout/Layout.vue'),
//     children: [
//       {
//         path: '/account/list',
//         meta: { title: '账号列表' },
//         component: () => import('../views/account/AccountList.vue')
//       },
//       {
//         path: '/account/change',
//         meta: { title: '修改密码' },
//         component: () => import('../views/account/ChangePwd.vue')
//       },
//       {
//         path: '/account/mine',
//         meta: { title: '个人中心' },
//         component: () => import('../views/account/Mine.vue')
//       }
//     ]
//   },
//   // {
//   //   path: '/*',
//   //   component: () => import('../views/error/error.vue')
//   // }
// ]


//超级管理员路由
// const superRoute = [
//   {
//     path: '/goods',
//     redirect: '/goods/list'
//   },
//   {
//     path: '/goods',
//     meta: { title: '商品管理' },
//     component: () => import('@/layout/Layout.vue'),
//     children: [
//       {
//         path: '/goods/add',
//         meta: { title: '商品添加' },
//         component: () => import('../views/goods/GoodsAdd.vue')
//       },
//       {
//         path: '/goods/list',
//         meta: { title: '商品列表' },
//         component: () => import('../views/goods/GoodsList.vue')
//       },
//       {
//         path: '/goods/classify',
//         meta: { title: '商品分类' },
//         component: () => import('../views/goods/GoodsClassify.vue')
//       }
//     ]
//   },
//   {
//     path: '/accout',
//     redirect: '/accout/list'
//   },
//   {
//     path: '/accout',
//     meta: { title: '账号管理' },
//     component: () => import('@/layout/Layout.vue'),
//     children: [
//       {
//         path: '/account/add',
//         meta: { title: '账号添加' },
//         component: () => import('../views/account/AccountAdd.vue')
//       },
//       {
//         path: '/account/list',
//         meta: { title: '账号列表' },
//         component: () => import('../views/account/AccountList.vue')
//       },
//       {
//         path: '/account/change',
//         meta: { title: '修改密码' },
//         component: () => import('../views/account/ChangePwd.vue')
//       },
//       {
//         path: '/account/mine',
//         meta: { title: '个人中心' },
//         component: () => import('../views/account/Mine.vue')
//       }
//     ]
//   },
//   {
//     path: '/order',
//     meta: { title: '订单' },
//     component: () => import('@/layout/Layout.vue'),
//     children: [
//       {
//         path: '',
//         // meta:{ title: '订单'},
//         component: () => import('../views/order/order.vue')
//       }
//     ]
//   },
//   {
//     path: '/sale',
//     meta: { title: '销售统计' },
//     component: () => import('@/layout/Layout.vue'),
//     children: [
//       {
//         path: '/sale/goods',
//         meta: { title: '商品统计' },
//         component: () => import('../views/sale/GoodsStatistics.vue')
//       },
//       {
//         path: '/sale/order',
//         meta: { title: '订单统计' },
//         component: () => import('../views/sale/OrderStatistics.vue')
//       },
//     ]
//   },
//   {
//     path: '/shop',
//     component: () => import('@/layout/Layout.vue'),
//     children: [
//       {
//         path: '',
//         meta: { title: '店铺管理' },
//         component: () => import('../views/shop/shop.vue')
//       }
//     ]
//   },
//   // {
//   //   path: '/*',
//   //   component: () => import('../views/error/error.vue')
//   // }
// ]

//动态路由
const dynamicRoutes = [
  // {
  //   path: '/goods',
  //   redirect: '/goods/list'
  // },
  {
    path: '/goods',
    meta: { title: '商品管理', roles: ['super', 'normal'] },
    component: () => import('@/layout/Layout.vue'),
    children: [
      {
        path: '/goods/add',
        meta: { title: '商品添加', roles: ['super'] },
        component: () => import('../views/goods/GoodsAdd.vue')
      },
      {
        path: '/goods/list',
        meta: { title: '商品列表', roles: ['super', 'normal'] },
        component: () => import('../views/goods/GoodsList.vue')
      },
      {
        path: '/goods/classify',
        meta: { title: '商品分类', roles: ['super', 'normal'] },
        component: () => import('../views/goods/GoodsClassify.vue')
      }
    ]
  },
  // {
  //   path: '/accout',
  //   redirect: '/accout/list'
  // },
  {
    path: '/accout',
    meta: { title: '账号管理', roles: ['super', 'normal'] },
    component: () => import('@/layout/Layout.vue'),
    children: [
      {
        path: '/account/add',
        meta: { title: '账号添加', roles: ['super'] },
        component: () => import('../views/account/AccountAdd.vue')
      },
      {
        path: '/account/list',
        meta: { title: '账号列表', roles: ['super', 'normal'] },
        component: () => import('../views/account/AccountList.vue')
      },
      {
        path: '/account/change',
        meta: { title: '修改密码', roles: ['super', 'normal'] },
        component: () => import('../views/account/ChangePwd.vue')
      },
      {
        path: '/account/mine',
        meta: { title: '个人中心', roles: ['super', 'normal'] },
        component: () => import('../views/account/Mine.vue')
      }
    ]
  },
  {
    path: '/order',
    meta: { title: '订单', roles: ['super'] },
    component: () => import('@/layout/Layout.vue'),
    children: [
      {
        path: '',
        meta: { title: '订单', roles: ['super'] },
        component: () => import('../views/order/order.vue')
      }
    ]
  },
  {
    path: '/sale',
    meta: { title: '销售统计', roles: ['super'] },
    component: () => import('@/layout/Layout.vue'),
    children: [
      {
        path: '/sale/goods',
        meta: { title: '商品统计', roles: ['super'] },
        component: () => import('../views/sale/GoodsStatistics.vue')
      },
      {
        path: '/sale/order',
        meta: { title: '订单统计', roles: ['super'] },
        component: () => import('../views/sale/OrderStatistics.vue')
      },
    ]
  },
  {
    path: '/shop',
    meta: { title: '店铺管理', roles: ['super'] },
    component: () => import('@/layout/Layout.vue'),
    children: [
      {
        path: '',
        meta: { title: '店铺管理', roles: ['super'] },
        component: () => import('../views/shop/shop.vue')
      }
    ]
  },
]



const router = new VueRouter({
  routes: staticRoutes
})




// 传入一个路由列表，再传入一个角色。 我返回一个过滤之后的路由列表 
export function filterRoute(routes, role) {
  return routes.filter(route => {
    //如果不止一层，有子路由的话，递归
    if (route.children && route.children.length > 0) {
      route.children = filterRoute(route.children, role)
    }
    return route.meta.roles.includes(role)
  })
}

//将过滤后的路由传入
export function addRoute() {
  let role = sessionStorage.getItem('role')
  if (!role) return
  let routeList = filterRoute(dynamicRoutes, role)
  routeList.forEach(route => {
    router.addRoute(route)
  })
  router.addRoute(
    {
      path: '/accout',
      redirect: '/accout/list'
    },
  )
  router.addRoute({
    path: '/goods',
    redirect: '/goods/list'
  })
  router.addRoute({
    path: '/*',
    component: () => import('../views/error/error.vue')
  })
}


//根据角色拿到菜单数据
export function checkMenu() {
  const role = sessionStorage.getItem('role')
  // debugger
  if (!role) return []
  // 把静态路由里面打了标的route拿出来
  let arr = staticRoutes.filter(route => route.meta && route.meta.menu)
  // debugger
  return [...arr, ...filterRoute(dynamicRoutes, role)]
}

//保证刷新会显示路由
addRoute()

//设置一个白名单
const whiteList = ['/login']

//全局前置路由守卫  to：要进入的路由  from：正要离开的路由  next：是否放行
router.beforeEach((to, from, next) => {
  if (whiteList.includes(to.fullPath)) return next()

  let token = sessionStorage.getItem('token')
  if (token) {
    next()
  } else {
    console.log('111111');
    next('/login')
  }
})

export default router
